﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using AnesSystem.Services;

namespace AnesSystem.ViewModels
{
    public class YaoPingHaoCaiCtrlViewModel : BaseViewModel<YaoPingHaoCaiCtrlViewModel>
    {
        #region Variables
        private AnesSystem.Models.Model.Hospital.CQ.CQNT.PatientLinkModel _patientLinkModel;

        private ObservableCollection<ConfigConsumablesModel> _disHaoCaiLeftList = new ObservableCollection<ConfigConsumablesModel>();

        private ObservableCollection<ConfigConsumablesModel> _disHaoCaiRightList = new ObservableCollection<ConfigConsumablesModel>();

        private ConfigConsumablesModel model = null;

        private StringBuilder mzysStr = new StringBuilder();

        private StringBuilder jbysStr = new StringBuilder();

        private int itemCount = 17;

        #endregion


        #region Constructor

        public YaoPingHaoCaiCtrlViewModel()
        {
            //UpdateDisplayOprList();
        }

        #endregion

        #region Properties

        public PatientLinkModel PatientLinkModel
        {
            get
            {
                return _patientLinkModel;
            }

            set
            {
                _patientLinkModel = value as AnesSystem.Models.Model.Hospital.CQ.CQNT.PatientLinkModel;
                BindHaoCaiList();
            }
        }


        public ObservableCollection<ConfigConsumablesModel> DisHaoCaiLeftList
        {
            get
            {
                return _disHaoCaiLeftList;
            }

            set
            {
                _disHaoCaiLeftList = value;
                RaisePropertyChanged("DisHaoCaiLeftList");
            }
        }

        public ObservableCollection<ConfigConsumablesModel> DisHaoCaiRightList
        {
            get
            {
                return _disHaoCaiRightList;
            }

            set
            {
                _disHaoCaiRightList = value;
                RaisePropertyChanged("DisHaoCaiRightList");
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods
        public void BindHaoCaiList()
        {
            DisHaoCaiLeftList.Clear();
            DisHaoCaiRightList.Clear();
            DisHaoCaiLeftList.Add(new ConfigConsumablesModel()
            {
                Name = "品名",
                Price = "价格",
                Count = "数量"
            });
            DisHaoCaiRightList.Add(new ConfigConsumablesModel()
            {
                Name = "品名",
                Price = "价格",
                Count = "数量"
            });

            if (_patientLinkModel.TableForNanTong.YaoPingHaoCai == null 
                || (_patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList.Count == 0 && _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList.Count == 0))
            {
                _patientLinkModel.TableForNanTong.YaoPingHaoCai = new CQNTYaoPingHaoCaiModel();
                UpdateDisplayOprList();
            }
            else
            {
                for (int i = 0; i < itemCount; i++)
                {
                    DisHaoCaiLeftList.Add(_patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList[i]);
                    DisHaoCaiRightList.Add(_patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList[i]);
                    DisHaoCaiLeftList[i].PropertyChanged += Model_PropertyChanged;
                    DisHaoCaiRightList[i].PropertyChanged += Model_PropertyChanged;
                }
            }
        }

        public void UpdateDisplayOprList()
        {
            List<ConfigConsumablesModel> consumablesModelList = DataManager.Instance.ConfigConsumablesModelList.ToList();
            List<ConfigConsumablesModel> leftList = consumablesModelList.Where(O => O.Col == 1).ToList();
            List<ConfigConsumablesModel> rightList = consumablesModelList.Where(O => O.Col == 2).ToList();
            leftList = leftList.OrderBy(O => O.Row).ToList();
            rightList = rightList.OrderBy(O => O.Row).ToList();
            for (int i = 0; i < itemCount; i++)
            {

                if (i >= leftList.Count())
                {
                    model = new ConfigConsumablesModel();
                }
                else
                {
                    model = leftList[i];
                }
                model.PropertyChanged += Model_PropertyChanged;
                DisHaoCaiLeftList.Add(model);
            }
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList = DisHaoCaiLeftList.ToList();
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList.RemoveAt(0);
            model = null;
            for (int i = 0; i < itemCount; i++)
            {

                if (i >= rightList.Count())
                {
                    model = new ConfigConsumablesModel();
                }
                else
                {
                    model = rightList[i];
                }
                model.PropertyChanged += Model_PropertyChanged;
                DisHaoCaiRightList.Add(model);
            }
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList = DisHaoCaiRightList.ToList();
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList.RemoveAt(0);
            PatientLinkService.Instance.AddUpdate(_patientLinkModel);
        }

        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList = DisHaoCaiLeftList.ToList();
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList = DisHaoCaiRightList.ToList();
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiRightList.RemoveAt(0);
            _patientLinkModel.TableForNanTong.YaoPingHaoCai.HaoCaiLeftList.RemoveAt(0);
            PatientLinkService.Instance.AddUpdate(_patientLinkModel);
        }



        #endregion

        #region EventHandlers

        #endregion
    }
}
